﻿using Net_SCADA.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class Form_AlarmLevelColorSet : fdBaseForm
    {
        

        public Form_AlarmLevelColorSet()
        {
            InitializeComponent();
            formLanguageIndex =-1;
    }

        private void btn_Close_Click(object sender , EventArgs e)
        {
            this.Dispose();
        }

        private void LoadInterface()
        {
            grd_ColorConfig.Columns["Col0"].Width = 100;
            grd_ColorConfig.Columns["Col1"].Width = (grd_ColorConfig.Width-100) /2;
            grd_ColorConfig.Columns["Col2"].Width = grd_ColorConfig.Width - 100 - grd_ColorConfig.Columns["Col1"].Width;

        }
        private void LoadColorData()
        {
            if (NetLine.AlarmLevelColor == null || NetLine.AlarmLevelColor.Count == 0) return;
            grd_ColorConfig.Rows.Clear();
            grd_ColorConfig.Rows.Add(NetLine.AlarmLevelColor.Count);
            int rowindex=0;
            foreach (int item in NetLine.AlarmLevelColor.Keys)
            {
                grd_ColorConfig.Rows[rowindex].Cells[0].Value = item.ToString();
                grd_ColorConfig.Rows[rowindex].Cells[1].Value = "";
                grd_ColorConfig.Rows[rowindex].Cells[2].Value = "";
                grd_ColorConfig.Rows[rowindex].Cells[1].Style.BackColor = NetLine.AlarmLevelColor[item][0];
                grd_ColorConfig.Rows[rowindex].Cells[2].Style.BackColor = NetLine.AlarmLevelColor[item][1];
                rowindex++;
            }
            grd_ColorConfig.Rows[0].Cells[0].Selected = true;
            lbl_ColorConfigTest.BackColor = grd_ColorConfig.Rows[0].Cells[2].Style.BackColor;
            lbl_ColorConfigTest.ForeColor = grd_ColorConfig.Rows[0].Cells[1].Style.BackColor;
        }

        private void Form_AlarmLevelColorSet_Load(object sender , EventArgs e)
        {
            LoadInterface();
            LoadColorData();
        }

        private void grd_ColorConfig_CellClick(object sender , DataGridViewCellEventArgs e)
        {
            int rowindex=grd_ColorConfig.CurrentCell.RowIndex;
            if (rowindex >= 0)
            {
                lbl_ColorConfigTest.ForeColor = grd_ColorConfig.Rows[rowindex].Cells[1].Style.BackColor;
                lbl_ColorConfigTest.BackColor = grd_ColorConfig.Rows[rowindex].Cells[2].Style.BackColor;
                grd_ColorConfig.Rows[rowindex].Cells[0].Selected = true;
            }
        }

        private void grd_ColorConfig_CellDoubleClick(object sender , DataGridViewCellEventArgs e)
        {
            int rowindex=grd_ColorConfig.CurrentCell.RowIndex;
            int colindex=grd_ColorConfig.CurrentCell.ColumnIndex;

            if (rowindex >= 0 && (colindex == 1 || colindex == 2))
            {
                ColorDialog cd=new ColorDialog();
                cd.Color = grd_ColorConfig.CurrentCell.Style.BackColor;
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    grd_ColorConfig.CurrentCell.Style.BackColor = cd.Color;
                    lbl_ColorConfigTest.ForeColor = grd_ColorConfig.Rows[rowindex].Cells[1].Style.BackColor;
                    lbl_ColorConfigTest.BackColor = grd_ColorConfig.Rows[rowindex].Cells[2].Style.BackColor;
                }
            }
            grd_ColorConfig.Rows[rowindex].Cells[0].Selected = true;
        }

        private void btn_Save_Click(object sender , EventArgs e)
        {
            for (int i = 0 ; i < grd_ColorConfig.Rows.Count ; i++)
            {
                int dictindex=Convert.ToInt32(grd_ColorConfig.Rows[i].Cells["Col0"].Value.ToString());
                Color color=grd_ColorConfig.Rows[i].Cells[1].Style.BackColor;
                NetLine.AlarmLevelColor[dictindex][0] = color;
                color = grd_ColorConfig.Rows[i].Cells[2].Style.BackColor;
                NetLine.AlarmLevelColor[dictindex][1] = color;
            }
            if (DBOperate.SaveAlarmLevelColorSet(NetLine.AlarmLevelColor))
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[0],"",formLanguageIndex);
            else
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[1],"",formLanguageIndex);
            this.Dispose();
        }

        private void Form_AlarmLevelColorSet_Activated(object sender, EventArgs e)
        {
            if (MultipleLanguage.systemLanguageIndex != formLanguageIndex)
            {
                formLanguageIndex = MultipleLanguage.systemLanguageIndex;
            }
        }
    }
}
